<template>
  <div>
    <PageWrapper :contentFullHeight="true" contentBackground>
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button
            v-auth="['documentManager:directory:create']"
            type="primary"
            @click="handleAdd"
            >{{ t('common.create') }}</a-button
          >
          <a-button
            v-auth="['documentManager:directory:batchDelete']"
            danger
            @click="batchDeletion"
            >{{ t('common.batchDelete') }}</a-button
          >
          <a-button type="primary" @click="expandAll">{{ t('common.expandAll') }}</a-button>
          <a-button type="primary" @click="collapseAll">{{ t('common.collapseAll') }}</a-button>
        </template>
        <!-- 操作 -->
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                icon: 'material-symbols:add-box-outline',
                tooltip: t('common.add'),
                onClick: handleAdd.bind(null, record),
                auth: 'documentManager:directory:create',
              },
              {
                icon: 'material-symbols:edit-square-outline-rounded',
                tooltip: t('common.edit'),
                onClick: handleEdit.bind(null, record),
                auth: 'documentManager:directory:update',
              },
              {
                icon: 'ion:trash-outline',
                tooltip: t('common.delete'),
                color: 'error',
                popConfirm: {
                  title: t('common.confirmDelete'),
                  placement: 'top',
                  confirm: handleDelete.bind(null, record),
                },
                auth: 'documentManager:directory:delete',
              },
            ]"
          />
        </template>
      </BasicTable>
      <!-- 目录新增修改 -->
      <directoryModal @register="registerDireModal" @success="reload" />
    </PageWrapper>
  </div>
</template>

<script setup lang="ts">
  import { getDirectoryLevel, getPrdFolder, deletePrdFolder } from '/@/api/demo/document';
  import { ref } from 'vue';
  import { PageWrapper } from '/@/components/Page';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { getDirectoryColumns } from './tableData';
  import { useMessage } from '/@/hooks/web/useMessage';

  import directoryModal from '../directoryModal.vue';

  const { t } = useI18n();
  const { createMessage } = useMessage();
  const superId = ref<number>(0);

  // 新增
  const handleAdd = async (record: Recordable) => {
    openDireModal(true, { treeKey: record.key, isUpdate: false });
  };

  // 修改
  const handleEdit = async (record: Recordable) => {
    getPrdFolder(record.key).then((res) => {
      openDireModal(true, { treeKey: record.key, isUpdate: true, res });
    });
  };

  // 删除
  const handleDelete = async (record: Recordable) => {
    deletePrdFolder(record.key).then(() => {
      reload();
      createMessage.success(t('common.delSuccessText'));
    });
  };

  // 批量删除
  const batchDeletion = () => {
    const selectedRowKeys = getSelectRowKeys();
    console.log('批量删除', selectedRowKeys);
  };

  // 表格初始化
  const [registerTable, { expandAll, collapseAll, getSelectRowKeys, reload }] = useTable({
    titleHelpMessage: 'docDirIndex',
    api: () => getDirectoryLevel(superId.value),
    rowKey: 'key',
    columns: getDirectoryColumns(),
    bordered: true,
    isTreeTable: true,
    showIndexColumn: false,
    showTableSetting: true, // 显示表格设置工具
    tableSetting: { fullScreen: true }, // 表格设置工具配置
    maxHeight: 600,
    striped: false,
    rowSelection: { type: 'checkbox' },
    actionColumn: {
      width: 200,
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  // 弹窗
  const [registerDireModal, { openModal: openDireModal }] = useModal();
</script>

<style lang="less" scoped></style>
